<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2018/7/24
 * Time: 下午11:22
 */
namespace PddWechat\Models\Share\Service;

use Dai\Framework\Base\BaseException;
use Dai\Framework\Base\BaseService;
use Dai\Framework\Base\BaseSessionInfo;
use PddWechat\Models\Share\Data\SharePosData;
use PddWechat\Models\Share\DataObject\SharePosDo;
use PddWechat\Models\Share\Param\SharePosParam;

class SharePos extends BaseService
{
    public function __construct()
    {
        $this->sharePosData = new SharePosData();
    }

    public function execute($basePageInfo)
    {
        /** @var BaseSessionInfo $sessionData */
        $sessionData = $basePageInfo->sessionInfo;
        /** @var SharePosParam $params */
        $params = $basePageInfo->params;
        $sharePosDo = new SharePosDo();
        $invateOpenId = $params->invateOpenId;
        $recommendOpenId = $sessionData->openId;
        $sharePosInfo = $this->sharePosData->selectShareInfoByInvIdAndRecomId($invateOpenId,$recommendOpenId);
        if($sharePosInfo != null) {
            throw new BaseException(BaseException::SHARE_RECORD_ALREADY_EXIST);
        }
        $sharePosDo->invateOpenId = $invateOpenId;
        $sharePosDo->recommendOpenId = $recommendOpenId;
        return $this->sharePosData->insert($sharePosDo);
    }
}